package com.example.springboot.service.impl;

import com.example.springboot.mapper.EmailMapper;
import com.example.springboot.model.Email;
import com.example.springboot.model.EmailExample;
import com.example.springboot.service.EmailService;
import com.example.springboot.util.EmailUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class EmailServiceImpl implements EmailService {
    @Autowired private EmailMapper emailMapper;
    @Override
    public Page<List<Email>> getListPage(Email email, Integer pageNum, Integer pageSize) {
        EmailExample example=new EmailExample();
        if(!StringUtils.isEmpty(email.getSendAddr())){
            example.createCriteria().andSendAddrLike("%"+email.getSendAddr()+"%");
        }
        Page<List<Email>> page=PageHelper.startPage(pageNum,pageSize);
        emailMapper.selectByExampleWithBLOBs(example);
       return page;
    }

    @Override
    public void sendEmail(Email e) {
        e.setType(2);//设置邮件类型
        emailMapper.insert(e);
        EmailUtil.send(e.getReviceAddr(), e.getEmailContent(),e.getTitle());
    }
}
